package com.zx.order.Services.Impl;

import com.zx.order.Services.OrderService;
import com.zx.order.entity.Order;
import com.zx.product.entity.Product;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

@Slf4j
@Service
public class OrderServiceImpl implements OrderService {


    @Autowired
    private DiscoveryClient  discoveryClient;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private LoadBalancerClient  loadBalancerClient;

    /**
     * 根据id获取订单信息
     * @param productId
     * @param userId
     * @return
     */
    @Override
    public Order createOrder(Long productId, Long userId) {
        log.info("开始获取订单信息");
        Product product = getProductFromRemoteWithBalanceAnnotation(productId);
        Order order = new Order();
        order.setUserId(userId);
        order.setId(1L);
        order.setAddress("北京");
        order.setNickName("张三");
        //总金额
        order.setTotalAmount(product.getPrice().multiply(new BigDecimal(product.getNum())));
        order.setProductList(Arrays.asList(product));
        return order;
    }

    /**
     * 远程获取商品信息
     * @param productId
     * @return
     */
    public Product getProductFromRemote(Long productId){
        //1.找到商品服务所在的所有机器的ip+port
        List<ServiceInstance> instances = discoveryClient.getInstances("service-product");
        //获取ip和port
        ServiceInstance serviceInstance = instances.get(0);
        String host = serviceInstance.getHost();
        int port = serviceInstance.getPort();
        //http://localhost:8080/products/getById/1
        String url  = "http://"+host+":"+port+"/products/getById/"+productId;
        log.info("远程请求路径为:{}",url);

        //给远程发送请求
        Product product = restTemplate.getForObject(url, Product.class);
        return product;
    }

    /**
     * 负载均衡获取商品信息
     * @param productId
     * @return
     */
    public Product getProductFromRemoteWithBalanceAnnotation(Long productId){
        String url  = "http://service-product/products/getById/"+productId;
        log.info("远程请求路径为:{}",url);

        //给远程发送请求
        Product product = restTemplate.getForObject(url, Product.class);
        return product;
    }

    /**
     * 基于注解的负载均衡获取商品信息
     * @param productId
     * @return
     */
    public Product getProductFromRemoteWithBalance(Long productId){
        //1.找到商品服务所在的所有机器的ip+port
        ServiceInstance choose = loadBalancerClient.choose("service-product");
        String host = choose.getHost();
        int port = choose.getPort();
        //http://localhost:8080/products/getById/1
        String url  = "http://"+host+":"+port+"/products/getById/"+productId;
        log.info("远程请求路径为:{}",url);

        //给远程发送请求
        Product product = restTemplate.getForObject(url, Product.class);
        return product;
    }
}
